package com.miaomi.Util;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

public class jdbcUtil {
    private static DataSource dataSource ;

    static {
        try {
            Properties properties = new Properties();
            InputStream resourceAsStream = jdbcUtil.class.getResourceAsStream("/lib.properties");
            properties.load(resourceAsStream);

            dataSource= DruidDataSourceFactory.createDataSource(properties);

        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static Connection getconnection() throws SQLException {
        return dataSource.getConnection();
    }

    public static void closeAll(Connection conn, PreparedStatement statement, ResultSet resultSet) throws SQLException {
        if (conn != null){
            conn.close();
        }
        if (statement != null){
            statement.close();
        }
        if (resultSet != null){
            resultSet.close();
        }
    }
}
